/*
 * Copyright 2021-2030 itbox.cn All right reserved. This software is the confidential and proprietary information
 * of itbox.cn ("Confidential Information"). You shall not disclose such Confidential Information and shall use it
 * only in accordance with the terms of the license agreement you entered into with itbox.cn.
 */
package cn.itbox.example.test.controller;

import cn.hutool.core.date.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @BelongProject springboot-study
 * @BelongPackage cn.itbox.example.controller
 * @Description TODO
 * @ClassName HomeController
 * @Author xingc
 * @Date 2023/11/26 17:09
 * @Version 1.0.0
 */
@Slf4j
@RestController
public class HomeController implements ApplicationContextAware {
    @Value("${spring.cloud.nacos.server-addr}")
    private String nacosServer;

    @GetMapping("/home")
    public Map index() {
        Map<String, String> map = new HashMap<>();
        map.put("welcome", String.format("Welcome to %s[%s] at %s", context.getId(), getActiveProfile(), DateUtil.now()));
        map.put("nacosAddr", nacosServer);
        return map;
    }

    private static ApplicationContext context;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        context = applicationContext;
    }
    private String getActiveProfile() {
        return context.getEnvironment().getActiveProfiles()[0];
    }
}
